//
//  AddAlipayVC.m
//  WalletAPP
//
//  Created by 罗弟祥 on 2019/10/10.
//  Copyright © 2019 罗弟祥. All rights reserved.
//

#import "AddAlipayVC.h"
#import "MyCenterModel.h"
@interface AddAlipayVC ()<UINavigationBarDelegate, UIImagePickerControllerDelegate>
@property (nonatomic, retain) NaviView *naviView;
@property (nonatomic, retain) RefreshVM *vm;

@property (weak, nonatomic) IBOutlet UIButton *codeBtn;
@property (weak, nonatomic) IBOutlet UITextField *nameTF;
@property (weak, nonatomic) IBOutlet UILabel *payType;
@property (weak, nonatomic) IBOutlet UITextField *payNoTF;
@property (weak, nonatomic) IBOutlet UITextField *passwordTF;


@property (nonatomic, copy) NSString *imgUrl;


@end

@implementation AddAlipayVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    weakself
    [self.view addSubview:self.naviView];
    self.naviView.backgroundColor = DEF_0xColor(0xEFEFF4, 1);
    self.naviView.frame = CGRectMake(0, 0, screenW, StatusBarHeight + NavBarHeight);
    self.naviView.titStr = @"Add Alipay";
    [self.naviView.rightBtn setTitle:@"Down" forState:UIControlStateNormal];
    [self.naviView.rightBtn addTarget:self action:@selector(rightBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    self.naviView.rightBtn.hidden = NO;
    [self.naviView setBackBlock:^{
        
        [ws.navigationController popViewControllerAnimated:YES];
    }];
 
    self.payType.text = self.payTypeStr;
    
}


-(void)rightBtnClick:(UIButton *)sender{
    if (self.nameTF.text.length < 1) {
        [Utils showHudWithTitle:@"请输入姓名" delay: 1.0 target:self];
        return;
    }
    if (self.payNoTF.text.length < 1) {
        [Utils showHudWithTitle:@"请输入账号" delay: 1.0 target:self];
        return;
    }
    if (self.imgUrl.length < 1) {
        [Utils showHudWithTitle:@"请上传收款码" delay: 1.0 target:self];
        return;
    }
    
    [self configViewModel];
}

-(void)configViewModel{
    
    __weak __typeof(&*self) ws = self;
    self.vm = [RefreshVM new];
    self.vm.urlStr = @"/api/payments";
    self.vm.params = [self configParam];
    self.vm.dataClass = [MyCenterModel class];
    [self.vm requestDataNormal];
    [self.vm setRequestResultBlock:^(id obj) {
        if (ws.addSuccessBlcok) {
            ws.addSuccessBlcok();
        }
        [ws.navigationController popViewControllerAnimated:YES];
    }];
}
-(NSDictionary *)configParam{
    return @{@"name": self.nameTF.text, @"account":self.payNoTF.text, @"type":self.payTypeStr, @"url":_imgUrl};
}

-(NaviView *)naviView{
    if (!_naviView) {
        _naviView = [[NSBundle mainBundle] loadNibNamed:NSStringFromClass([NaviView class]) owner:self options:nil].firstObject;
    }
    return _naviView;
}
- (IBAction)codeBtnClick:(UIButton *)sender {
    [PickPicture getPickPictureWithViewControll:self];
}

#pragma Delegate method UIImagePickerControllerDelegate
//图像选取器的委托方法，选完图片后回调该方法
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingImage:(UIImage *)image editingInfo:(NSDictionary *)editingInfo{
    //NSLog(@"image:%@，%@", image, editingInfo);
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //把新拍的照片保存到相册
        UIImageWriteToSavedPhotosAlbum(editingInfo[UIImagePickerControllerOriginalImage], nil, nil, nil);
    }
    NSData *data;
    
    if (image != nil) {
        
        data = UIImageJPEGRepresentation(image, 0.3);
        
    }
    [NetWork uploadWithUrl:[NSString stringWithFormat:@"%@/api/images?type=%@", MAIN_URL, @"payment"] Image:image successBlock:^(id responseObj) {
        self.imgUrl = responseObj[@"path"];
        [self.codeBtn setImage:image forState:UIControlStateNormal];
        
    }];
    
    //关闭相册界面
    [picker dismissViewControllerAnimated:YES completion:nil];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
